package com.dmwork.service.interaction.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmwork.service.interaction.entity.WorkCollect;
import com.dmwork.service.interaction.entity.vo.WorkCollectAddVo;
import com.dmwork.service.interaction.feign.WorkFeignService;
import com.dmwork.service.interaction.mapper.WorkCollectMapper;
import com.dmwork.service.interaction.service.WorkCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 李天
 * @since 2021-03-20
 */
@Service
public class WorkCollectServiceImpl extends ServiceImpl<WorkCollectMapper, WorkCollect> implements WorkCollectService {

    @Autowired
    private WorkCollectMapper workCollectMapper;

    @Override
    public void removeCollect(WorkCollect workCollect) {
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workCollect.getWorkId())
                .eq("customer_id", workCollect.getCustomerId())
                .eq("collectGroupId", workCollect.getCollectGroupId());
        baseMapper.delete(queryWrapper);
    }

    @Override
    public Page<WorkCollect> getWorkIdPage(WorkCollect workCollect, Integer page, Integer limit) {
        Page<WorkCollect> selectPage = new Page<>(page, limit);
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collect_group_id", workCollect.getCollectGroupId())
                .eq("customer_id", workCollect.getCustomerId());
        selectPage = baseMapper.selectPage(selectPage, queryWrapper);
        return selectPage;
    }

    @Override
    public void removeBatchByCollectGroup(WorkCollect workCollect) {
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("collect_group_id", workCollect.getCollectGroupId())
                .eq("customer_id", workCollect.getCustomerId());
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void removeBatchCollect(String customerId, String collectGroupId, List<String> workIdList) {
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId)
                .eq("collect_group_id", collectGroupId)
                .in("work_id", workIdList);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void addBatchCollect(WorkCollectAddVo workCollectAddVo, String customerId) {
        List<WorkCollect> workCollectList = new ArrayList<>();
        for (String collectGroupId : workCollectAddVo.getCollectGroupIdList()) {
            WorkCollect workCollect = new WorkCollect();
            workCollect.setCustomerId(customerId);
            workCollect.setWorkId(workCollectAddVo.getWorkId());
            workCollect.setCollectGroupId(collectGroupId);
            workCollectList.add(workCollect);
        }
        this.saveBatch(workCollectList);
    }

    @Override
    public Integer getCollectCountByWorkId(String workId) {
        return baseMapper.getCollectCountByWorkId(workId);
    }

    @Override
    public Map<String, Integer> getBatchCollectCountByWorkIdList(List<String> workIdList) {
        Map<String, Integer> resultMap = new HashMap<>();
        QueryWrapper<WorkCollect> queryWrapper = new QueryWrapper<>();
        //先查出带有这些workId的所有记录
        queryWrapper.in("work_id", workIdList);
        List<WorkCollect> workCollectList = baseMapper.selectList(queryWrapper);

        //对每个作品统计收藏数
        int count;
        for (String workId : workIdList) {
            count = 0;
            for (WorkCollect workCollect : workCollectList) {
                if (workId.equals(workCollect.getWorkId())) {
                    count++;
                }
            }
            resultMap.put(workId, count);
        }

        return resultMap;
    }

    @Override
    public Integer getCollectCountByCustomerId(String customerId) {
        return baseMapper.getCollectCountByCustomerId(customerId);
    }

    @Override
    public void moveBatchCollect(String customerId, String collectGroupId, String originalGroupId, List<String> workIdList) {
        /**
         *对于某一个作品，该作品收藏到了多个分组中，若要将其从一个分组移动到另一个分组中，而该分组中已经有该作品，则只需要把移动的原分组的该作品记录删除；
         * 若目标分组没有该作品，则将分组记录中的原分组id改为目标分组id
         */
        QueryWrapper<WorkCollect> queryWrapper;
        //对于目标分组已有该作品的，需要将原分组的该作品记录删除
        List<String> deleteWorkIdList = workCollectMapper.selectWorkIdListByQuery(customerId, collectGroupId, workIdList);
        int deleteCount = 0;
        if (deleteWorkIdList.size() > 0) {
            queryWrapper = new QueryWrapper<WorkCollect>()
                    .eq("customer_id", customerId)
                    .eq("collect_group_id", originalGroupId)
                    .in("work_id", deleteWorkIdList);
            deleteCount = baseMapper.delete(queryWrapper);
        }

        if (deleteCount < workIdList.size()) {
            //对于目标分组没有该作品的，要修改原分组id为目标分组id
            WorkCollect workCollect = new WorkCollect();
            workCollect.setCollectGroupId(collectGroupId);
            queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("customer_id", customerId)
                    .eq("collect_group_id", originalGroupId)
                    .in("work_id", workIdList);
            baseMapper.update(workCollect, queryWrapper);
        }
    }
}
